{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1a5df138",
   "metadata": {},
   "source": [
    "# 1. 利用python函数的方法，封装 face++ 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dd4e0b71",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c99f9712",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'iE1MiracWI1O1jmSAZWUrhdS7FOvV8My'\n",
    "API_secret = '8B16HiL0padJMA1cBvkYCU0lPJ8hqZ_l'\n",
    "img_01 = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F10985943880%2F641.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650870232&t=86ab568cb4ad2ab2a9917af637c67650'\n",
    "img_02 = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fqqpublic.qpic.cn%2Fqq_public%2F0%2F0-2587108314-6594DB0995FBBA388B26C2E35C730CDE%2F0%3Ffmt%3Djpg%26amp%3Bsize%3D45%26amp%3Bh%3D797%26amp%3Bw%3D512%26amp%3Bppv%3D1&refer=http%3A%2F%2Fqqpublic.qpic.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650870232&t=fbf52493f18616b79f2052950e693b68'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "df093dec",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f70d8a65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648396746,4d9f4702-ae72-4f28-aa92-2bb0079697ad',\n",
       " 'time_used': 174,\n",
       " 'faces': [{'face_token': '5034c893f059227dc1365252c1eb3fe2',\n",
       "   'face_rectangle': {'top': 261, 'left': 223, 'width': 146, 'height': 146}}],\n",
       " 'image_id': 'SrjRxMmQoxZM1DBFftvmgQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_secret,img_01)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10fc29e7",
   "metadata": {},
   "source": [
    "# 2. 利用python函数的方法，封装 face++ 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "cef165f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "def face_analyze(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸分析函数\"\"\"\n",
    "    analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url,\n",
    "        'face_tokens':face_token,\n",
    "        \"return_attributes\":'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    }\n",
    "    r_analyze = requests.post(analyze_url, params = payload)\n",
    "    return r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "bda616e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'58d715b736ca23df182986b9768da256'"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face01 = detect_face(API_key,API_secret,img_01)\n",
    "face01['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "614bd67a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cbce1ebfb2ef613ab098ac1497d51429'"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face02 = detect_face(API_key,API_secret,img_02)\n",
    "face02['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "7be68974",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_token01 = face01['faces'][0]['face_token']\n",
    "face_token02 = face02['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "3559d620",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 180,\n",
       " 'request_id': '1648399338,ac9d8666-e7a0-495f-acbd-a41e487cb81f',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.112,\n",
       "     'neutral': 1.039,\n",
       "     'disgust': 0.009,\n",
       "     'anger': 0.009,\n",
       "     'surprise': 0.024,\n",
       "     'fear': 0.298,\n",
       "     'happiness': 98.508},\n",
       "    'beauty': {'female_score': 83.602, 'male_score': 80.945},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 32},\n",
       "    'mouthstatus': {'close': 99.998,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.002,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 3.715,\n",
       "     'stain': 12.277,\n",
       "     'acne': 8.385,\n",
       "     'health': 31.553},\n",
       "    'headpose': {'yaw_angle': 15.14239,\n",
       "     'pitch_angle': 3.3096385,\n",
       "     'roll_angle': 6.1320534},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.032},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.032},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.032}},\n",
       "    'smile': {'threshold': 50.0, 'value': 98.639},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.164,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.836,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.058,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.942,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 55.557},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.498,\n",
       "      'vector_z_component': 0.997,\n",
       "      'vector_x_component': 0.043,\n",
       "      'vector_y_component': 0.057,\n",
       "      'position_y_coordinate': 0.408},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.55,\n",
       "      'vector_z_component': 0.98,\n",
       "      'vector_x_component': 0.191,\n",
       "      'vector_y_component': -0.051,\n",
       "      'position_y_coordinate': 0.366}}},\n",
       "   'face_rectangle': {'width': 146, 'top': 261, 'left': 223, 'height': 146},\n",
       "   'face_token': '4b623cf48db0465ba889735553c78a75'}]}"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_analyze(API_key,API_secret,face_token01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "ce187840",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 138,\n",
       " 'request_id': '1648399352,5f47e737-236e-44c5-91b4-656b0dc45963',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.112,\n",
       "     'neutral': 1.039,\n",
       "     'disgust': 0.009,\n",
       "     'anger': 0.009,\n",
       "     'surprise': 0.024,\n",
       "     'fear': 0.298,\n",
       "     'happiness': 98.508},\n",
       "    'beauty': {'female_score': 83.602, 'male_score': 80.945},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 32},\n",
       "    'mouthstatus': {'close': 99.998,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.002,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 3.715,\n",
       "     'stain': 12.277,\n",
       "     'acne': 8.385,\n",
       "     'health': 31.553},\n",
       "    'headpose': {'yaw_angle': 15.14239,\n",
       "     'pitch_angle': 3.3096385,\n",
       "     'roll_angle': 6.1320534},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.032},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.032},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.032}},\n",
       "    'smile': {'threshold': 50.0, 'value': 98.639},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.164,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.836,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.058,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.942,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 55.557},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.498,\n",
       "      'vector_z_component': 0.997,\n",
       "      'vector_x_component': 0.043,\n",
       "      'vector_y_component': 0.057,\n",
       "      'position_y_coordinate': 0.408},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.55,\n",
       "      'vector_z_component': 0.98,\n",
       "      'vector_x_component': 0.191,\n",
       "      'vector_y_component': -0.051,\n",
       "      'position_y_coordinate': 0.366}}},\n",
       "   'face_rectangle': {'width': 146, 'top': 261, 'left': 223, 'height': 146},\n",
       "   'face_token': '4b623cf48db0465ba889735553c78a75'}]}"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_analyze(API_key,API_secret,face_token02)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a6e46e1",
   "metadata": {},
   "source": [
    "# 3. 利用python函数的方法，封装 face++ 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "237127fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "def face_compare(API_key,API_secret,face_token1,face_token2):\n",
    "    '''face++ API人脸对比函数'''\n",
    "    compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "    payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"face_token1\":face_token1,\n",
    "    \"face_token2\":face_token2\n",
    "\n",
    "    }\n",
    "    r_compare = requests.post(url = compare_url , params = payload)\n",
    "    return r_compare.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "8c1e7371",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'7a8da503c20eee25f2b08ffdecf7c857'"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face1 = detect_face(API_key,API_secret,img_01)\n",
    "face1['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "2ae7cf73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'03ddbaa3024f3b0f530d77bb42db89dc'"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face2 = detect_face(API_key,API_secret,img_02)\n",
    "face2['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "bea6d1e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_token1 = face1['faces'][0]['face_token']\n",
    "face_token2 = face2['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "9b57ce26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648399803,68a04860-923f-46fe-ad5e-09da27085f5e',\n",
       " 'time_used': 312,\n",
       " 'confidence': 84.894,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_compare(API_key,API_secret,face_token1,face_token2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59e8e62f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
